package test13;

import com.tyfspring.aop.AdvisedSupport;
import com.tyfspring.aop.ClassFilter;
import com.tyfspring.aop.TargetSource;
import com.tyfspring.aop.aspectj.AspectJExpressionPointcut;
import com.tyfspring.aop.aspectj.AspectJExpressionPointcutAdvisor;
import com.tyfspring.aop.framework.ProxyFactory;
import com.tyfspring.aop.framework.adapter.MethodBeforeAdviceInterceptor;
import com.tyfspring.context.support.ClassPathXmlApplicationContext;
import org.aopalliance.intercept.MethodInterceptor;
import org.junit.Before;
import org.junit.Test;

/**
 * @Description TODO
 * @Author shallow
 * @Date 2023/4/18 18:58
 */

public class test {

    private AdvisedSupport advisedSupport;

    @Before
    public void init(){
        IUserService userService = new UserService();
        advisedSupport = new AdvisedSupport();
        advisedSupport.setTargetSource(new TargetSource(userService));
        advisedSupport.setMethodInterceptor(new UserServiceInterceptor());
        advisedSupport.setMethodMatcher(new AspectJExpressionPointcut("execution(* test13.IUserService.*(..))"));

    }

    @Test
    public void test_proxyFactory(){
        advisedSupport.setProxyTargetClass(false);
        IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
        System.out.println("proxy = " + proxy.queryUserInfo());
    }

    @Test
    public void test_beforeAdvice(){
        UserServiceBeforeAdvice beforeAdvice = new UserServiceBeforeAdvice();
        MethodBeforeAdviceInterceptor interceptor = new MethodBeforeAdviceInterceptor(beforeAdvice);
        advisedSupport.setMethodInterceptor(interceptor);
        IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
        System.out.println("proxy = " + proxy.queryUserInfo());
    }

    @Test
    public void test_advisor(){
        IUserService userService = new UserService();
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression("execution(* test13.IUserService.*(..))");
        advisor.setAdvice(new MethodBeforeAdviceInterceptor(new UserServiceBeforeAdvice()));

        ClassFilter classFilter = advisor.getPointCut().getClassFilter();
        if (classFilter.matches(userService.getClass())){
            AdvisedSupport advisedSupport = new AdvisedSupport();
            TargetSource targetSource = new TargetSource(userService);
            advisedSupport.setTargetSource(targetSource);
            advisedSupport.setMethodInterceptor((MethodInterceptor) advisor.getAdvice());
            advisedSupport.setMethodMatcher(advisor.getPointCut().getMethodMatcher());
            advisedSupport.setProxyTargetClass(true);

            IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
            System.out.println("proxy = " + proxy.queryUserInfo());
        }
    }

    @Test
    public void test_aop(){
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:test13/spring.xml");
        IUserService userService = classPathXmlApplicationContext.getBean("userService", IUserService.class);
        System.out.println("userService = " + userService.queryUserInfo());
    }
}
